<?php
namespace Game\Model;
use Think\Model;
/**
 * 助力排名表
 * @author huhaiqin
 */
class GameSupportRankModel extends Model{
    /**
     * 更新排行榜数据
     * @param $uid
     * @param $idol_id
     * @param $support
     * @param $mode : day,week,month
     */
    public function update_user_support($uid, $idol_id, $support, $mode){
        // 先验证合法
        if(!$uid || !$idol_id || !$support){
            return false;
        }
        // mode的合法校验
        $expire_time = $this->_get_mode_expire_time($mode);
        if(!$expire_time){
            return false;
        }
        // 每一次成功的support执行
        // 找出当前要参与的榜单
        $map = [
            'uid' => $uid,
            'idol_id' => $idol_id,
            'expire_time' => $expire_time,
            'mode' => $mode
        ];
        $res = $this->where($map)->find();
        if($res){
            // 存在记录，直接更新
            $data = [
                'support' => $res['support'] + $support,
                'update_time' => time()
            ];
            $this->where(['id'=>$res['id']])->save($data);
        }else{
            // 不存在记录，新增
            $data = $map;
            $data['support'] = $support;
            $data['update_time'] = time();
            $this->add($data);
        }
        // 把缓存数据更新
        $new_data = [
            'uid' => $uid,
            'support' => $data['support'],
            'update_time' => $data['update_time'],
        ];
        _update_user_rank_cache($new_data, $idol_id, $mode);
    }
    /**
     * 查询玩家在某个idol下的助力值
     * @param $uid
     * @param $idol_id
     * @param $mode
     * @return array|bool
     */
    public function get_user_support_info($uid, $idol_id, $mode){
        // 先验证合法
        if(!$uid || !$idol_id || !$mode){
            return false;
        }
        // mode的合法校验
        $expire_time = $this->_get_mode_expire_time($mode);
        if(!$expire_time){
            return false;
        }
        // 查询
        $map = [
            'uid' => $uid,
            'idol_id' => $idol_id,
            'expire_time' => $expire_time,
            'mode' => $mode
        ];
        $res = $this->where($map)->field('uid, idol_id, support, update_time')->find();
        if($res){
            return $res;
        }
        return false;
    }

    /**
     * 获取某个idol下的用户的排行榜数据，默认返回200条
     * 先排support高的，support相同的按更新时间先后排序
     * @param $idol_id
     * @param $mode
     * @param $limit
     * @param $type current 当前周期 last 上一个周期
     */
    public function get_idol_user_rank_list($idol_id, $mode, $limit = 200, $type = 'current'){
        // 先验证合法
        if(!$idol_id || !$mode){
            return false;
        }
        // mode的合法校验
        if($type == 'current'){
            $expire_time = $this->_get_mode_expire_time($mode);
        }elseif($type == 'last'){
            $expire_time = $this->_get_last_expire_time($mode);
        }
        if(!$expire_time){
            return false;
        }
        // 分页查询
        $map = [
            'idol_id' => $idol_id,
            'mode' => $mode,
            'expire_time' => $expire_time
        ];
        // 顺序根据助力值高低，更新时间先后来排序
        $list = $this->where($map)
            ->order('support desc, update_time asc')
            ->limit($limit)->field('uid, support, update_time')
            ->select();
        return $list;
    }
    
    /**
     * 获取不同IDOL间的排名
     */
    public function get_idol_rank_list($mode){
        // 先验证合法
        if(!$mode){
            return false;
        }
        // mode的合法校验
        $expire_time = $this->_get_mode_expire_time($mode);
        if(!$expire_time){
            return false;
        }
        $idol_rank = [];
        $idol_list = _get_config_table()->getTbIdol()->getDataList();
        $res = $this->field('idol_id, sum(support) as support')
            ->where(['mode'=>$mode, 'expire_time'=>$expire_time])
            ->group('idol_id')
            ->select();
        foreach($res as $v){
            $idol_rank[$v['idol_id']]['support'] = intval($v['support']);
        }
        foreach($idol_list as $v){
            if(!isset($idol_rank[$v->id]) && !$v->isFriend){
                $idol_rank[$v->id]['support'] = 0;
            }
        }
        uasort($idol_rank, function($a, $b){
            return $b['support'] <=> $a['support'];
        });
        $rank = 1;
        foreach($idol_rank as $k => $v){
            $idol_rank[$k]['rank'] = $rank ++;
        }
        return $idol_rank;
    }

    /**
     * 获取当前周期的过期时间
     * @param $mode
     */

    public function _get_mode_expire_time($mode){
        switch($mode){
            case 'day':
                return strtotime('tomorrow midnight');
            case 'week':
                return strtotime('next week 00:00:00');
            case 'month':
                return strtotime('first day of next month 00:00:00');
            default:
                return false;
        }
    }
    /**
     * 获取上一个周期的过期时间
     * @param $mode
     */
    public function _get_last_expire_time($mode){
        switch($mode){
            case 'day':
                return strtotime('today midnight');
            case 'week':
                return strtotime('this week 00:00:00');
            case 'month':
                return strtotime('first day of this month 00:00:00');
            default:
                return false;
        }
    }

    /**
     * 获取上一个周期的排名数据
     * 直接存缓存
     * @param $mode
     */
    public function get_last_rank_info($mode){
        // 先验证合法
        if(!$mode){
            return false;
        }
        // mode的合法校验
        $expire_time = $this->_get_last_expire_time($mode);
        if(!$expire_time){
            return false;
        }
        // 分明星查询
        $idol_rank = [];
        $idol_list = _get_config_table()->getTbIdol()->getDataList();
        $res = $this->field('idol_id, sum(support) as support')
            ->where(['mode'=>$mode, 'expire_time'=>$expire_time])
            ->group('idol_id')
            ->select();
        foreach($res as $v){
            $idol_rank[$v['idol_id']]['support'] = intval($v['support']);
        }
        foreach($idol_list as $v){
            // 先排除挚友卡
            if($v->isFriend){
                continue;
            }
            if(!isset($idol_rank[$v->id])){
                $idol_rank[$v->id]['support'] = 0;
            }
            // 查用户排名，默认200条
            $user_rank = $this->get_idol_user_rank_list($v->id, $mode, 200, 'last');
            $idol_rank[$v->id]['user_rank_list'] = $user_rank;
        }
        uasort($idol_rank, function($a, $b){
            return $b['support'] <=> $a['support'];
        });
        $rank = 1;
        foreach($idol_rank as $k => $v){
            $idol_rank[$k]['rank'] = $rank ++;
        }
        return $idol_rank;
    }
}